<include file="public@storage"/>
<link rel="stylesheet" href="__STATIC__/storage/js/bom/bom.css"/>
</head>
<body>

<div class="container body-content" style="margin-top:20px;width: 100%">
    <div class="panel panel-primary" style="margin-bottom: 0.5rem">
        <div class="panel-heading"><span id="storageName">{$storageInfo[0]['name']}</span>出库单查询<span
                id="condition"></span></div>
        <input type="hidden" id="storageUuid" value="{$storageInfo[0]['uuid']}">
        <div class="panel-body">
            <div class="row">
                <div class="col-sm-12">
                    <div id="toolbar" class="btn-group">
                        <select id="selectStorage" class="btn btn-info" style="margin-left: 5px; width: 10rem"
                                onchange="changeStorage()">
                            <option value="">请选择仓库</option>
                            <volist id="uuid" name="storageInfo" id="value">
                                <option value="{$value['uuid']}">{$value['name']}</option>
                            </volist>
                        </select>
                    </div>
                    <table id="table"></table>
                </div>
            </div>
        </div>
    </div>
    <div class='qrcode' id="qrcode" style="display: none"></div>
</div>
</body>
<!--生成二维码插件-->
<script src="__STATIC__/js/qrcode.min.js"></script>
<script src="__STATIC__/storage/js/common/selectStorage.js"></script>
<script>
    var index;
    var confirm;
    var storageUuid = $("#storageUuid").val();

    $(function () {
        if ($("#storageUuid").val() === '') {
            $("#selectStorage").show();
        } else {
            $("#selectStorage").hide();
        }
        init();
    });

    function init() {
        var url = "{:url('Bom/getBom')}?storageUuid=" + storageUuid + '&hidden=0';
        var columns = [
            {
                title: '序号', width: 50,
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
            {title: 'uuid', field: 'uuid', visible: false, searchable: false},
            {
                title: '单号',
                field: 'order_id',
                align: "center",
                valign: 'middle',
                filter: {
                    type: 'input',
                }
            },
            {
                title: '行号',
                field: 'fentryid',
                align: "center",
                valign: 'middle',
                width: 50, filter: {
                    type: 'input'
                }
            },
            {
                title: '备注',
                field: 'remark',
                align: "center",
                valign: 'middle',
                filter: {
                    type: 'input'
                }
            },
            {
                title: '生产车间',
                field: 'workshop',
                align: "center",
                valign: 'middle',
                width: 100, filter: {
                    type: 'input'
                }
            },
            {
                title: '生产类型',
                field: 'product_type',
                align: "center",
                valign: 'middle',
                width: 100, filter: {
                    type: 'input'
                }
            },
            {
                title: '委外厂商',
                field: 'company_name',
                align: "center",
                valign: 'middle',
                width: 150,
                filter: {
                    type: 'input'
                }
            },
            {
                title: '生成仓库',
                field: 'name',
                align: "center",
                valign: 'middle',
                filter: {
                    type: 'input'
                }
            },
            {
                title: '时间',
                field: 'time',
                align: "center",
                valign: 'middle',
                filter: {
                    type: 'input'
                }
            },

            {
                title: '状态',
                field: 'status',
                width: 150,
                align: "center",
                valign: 'middle',
                filter: {
                    type: 'select',
                    data: [{id: '', text: '未出库'}, {id: 0, text: '未出库'}, {id: 1, text: '已完成'}, {id: 2, text: '欠料'}, {
                        id: 3,
                        text: '未完成'
                    }]//原版未出库状态为NULL所以这边对原版迁移的数据进行一个兼容
                },
                formatter: function (value, row, index) {
                    var html = '';
                    if (value === 0) {
                        html += '<label class="undo">未出库</label>';
                    }
                    else if (value === 1) {
                        html += '<label class="finished">已完成</label>';
                    }
                    else if (value === 2) {
                        html += '<label class="owe">欠料</label>';
                    }
                    else if(value === 3){
                        html += '<label class="unfinished">未完成</label>';
                    }
                    else{
                        html += '<label class="undo">未出库</label>';
                    }
                    return html;
                }
            },
            {
                title: '操作',
                field: 'uuid',
                align: "center",
                valign: 'middle',
                formatter: selectList
            }
        ];
        initTable(url, columns, 'server', InitSubTable);
        setColumn();
    }

    InitSubTable = function (index, row, $detail) {
        var tableId = "cur_table_"+index;
        $detail.html('<table id="'+ tableId+ '"></table>').find('table');
        var bomUuid = row.uuid;
        var fentryid = row.fentryid;
        var orderId = row.order_id;
        var url = "{:url('Bom/getBomDetail')}?bomUuid=" + bomUuid + '&orderId=' + orderId + '&fentryid=' + fentryid + '&storageUuid=' + storageUuid;
        console.log(url);
        var column = [
            {
                title: '序号',
                width: 50,
                align: "center",
                valign: 'middle',
                searchable: false,
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
            {
                title: 'uuid',
                field: 'uuid',
                visible: false,
                searchable: false
            },
            {
                title: '物料号',
                field: 'sub_materiel_id',
                align: "center"
            },
            {
                title: '物料名称',
                field: 'sub_materiel_name',
                align: "center"
            },
            {
                title: '投料数量',
                field: 'count',
                align: "center"
            },
            {
                title: '账上库存',
                field: 's_count',
                align: "center"
            },
            {
                title: '架上库存',
                field: 'r_count',
                align: "center"
            },
            {
                title: '已发数量',
                field: 'send',
                align: "center"
            },
            {
                title: '投料减已发',
                field: 'diff',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value < 0) {
                        return '多发' + (0 - value);
                    } else if (value > 0) {
                        return '未发' + value;
                    } else {
                        return 0;
                    }
                }
            },
            {
                title: '齐套',
                field: 'kitting',
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '<label class="finished">齐套</label>';
                    } else {
                        return '<label class="undo">未齐套</label>';
                    }
                }
            }
        ];
        initSubTable(tableId,url, column, 'client', true);
    };


    //生成二维码
    printCode = function (that) {
        var orderId = $(that).data('order');
        var code = $('.qrcode');
        $('.qrcode canvas').remove();
        $('.qrcode img').remove();
        $('.qrcode p').remove();
        var qrcode = new QRCode(document.getElementById('qrcode'), {
            text: orderId,
            width: 256,
            height: 256
        });
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            area: ['300px', '300px'],
            skin: '', //没有背景色
            shadeClose: true,
            content: $('.qrcode')
        });
        var html = "<p class='q-qrcode'>" + orderId + "</p>";
        code.append(html);
    };

    function clean(that) {
        var orderId = $(that).data('order');
        confirm = layer.confirm('是否对该出库单结算？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: 'index.php?g=Output&m=Bom&a=clean',
                data: {bomId: orderId},
                type: 'post',
                dateType: 'json',
                beforeSend: function () {
                    layer.close(confirm);
                    index = layer.load(0, {shade: false});
                }, complete: function () {
                    layer.close(index);
                }, success: function (res) {
                    if (res.success === true) {
                        layer.msg(res.message);
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    } else if (res.success === false) {
                        layer.alert(res.message);
                    }
                }, error: function (e) {
                    console.log(e);
                }
            });
        }, function () {
            layer.close(confirm);
        });
    }

    function setColumn() {
        if (storageUuid === '') {
            $('#table').bootstrapTable('showColumn', 'name');
        } else {
            $('#table').bootstrapTable('hideColumn', 'name');
        }
    }

    var orderId = null;
    selectList = function (value, row) {
        var fen = row.fentryid ? ('+' + row.fentryid) : '';
        var uuid = row.uuid;
        return '<a class="print" onclick="printCode(this)" href="javascript:;" data-order="' + row.order_id + fen + '"  style="text-decoration:none">二维码</a>' +
            '&nbsp&nbsp' + '<a  href="javascript:;" onclick="carList(this)"  data-uuid="' + row.uuid + '" style="text-decoration:none">投料单</a>' +
            "<select  style='margin-left: 5px' onchange=\"listChange(this,'" + uuid + "')\">" +
            '<option value="0">其余操作</option>' +
            '<option value="1">重推</option>' +
            '<option value="2">完成</option>' +
            '<option value="3">结算</option>' +
            '<option value="4">删除</option>' +
            '</select>';
    };

    listChange = function (obj, uuid) {
        var caseValue = obj.selectedIndex;
        var row = $('#table').bootstrapTable('getRowByUniqueId', uuid);
        var fen = row.fentryid ? ('+' + row.fentryid) : '';
        orderId = row.order_id + fen;
        $('#table select').val(0);
        switch (caseValue) {
            case 1:
                rePush(row);
                break;
            case 2:
                setting(row);
                break;
            case 3:
                balance(row);
                break;
            case 4:
                deleteBom(row);
                break;
        }
    };

    function deleteBom(row) {
        var uuid = row.uuid;
        var tip = '是否删除' + orderId + '订单？';
        var url = "{:url('Bom/deleteBom')}";
        var data = {bomUuid: uuid};
        reviseTable(tip, url, data);
    }

    function setting(row) {
        var uuid = row.uuid;
        var status = row.status;
        if (status != 2) {
            layer.alert("只有在欠料状态下才能标记为完成!");
            return;
        }
        var tip = '是否将出库单' + orderId + '标记为完成？';
        var url = "{:url('Bom/setting')}";
        var data = {bomUuid: uuid};
        reviseTable(tip, url, data);
    }

    carList = function (that) {
        var bomUuid = $(that).data('uuid');
        var url = "{:url('Bom/car')}?history=1&bomUuid=" + bomUuid;
        parent.openapp(url,'小车清单','小车清单');
    };

    rePush = function (row) {
        var bomUuid = row.uuid;
        var data = {
            orderId: orderId,
            bomUuid: bomUuid
        };
        var tip = '是否重推出库单' + orderId;
        var url = "{:url('Bom/rePush')}";
        reviseTable(tip, url, data);
    };

    balance = function (row) {
        var bomUuid = row.uuid;
        var data = {
            orderId: orderId,
            bomUuid: bomUuid
        };
        var tip = '是否结算出库单' + orderId;
        var url = "{:url('Bom/balance')}";
        reviseTable(tip, url, data);
    }
</script>
</html>